package me.zhengjie.modules.gis.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequiredArgsConstructor
@Api(tags = "定位系统测试接口")
@RequestMapping("/v1")
public class PositionTestController {

    //轨迹
    public static final List<BigDecimal[]> trailList = new ArrayList<>();
    //定位列表
    public static final List<BigDecimal[]> positionList = new ArrayList<>();

    static {
        //轨迹
        BigDecimal[] arr = new BigDecimal[2];
        arr[0] = new BigDecimal("120.449597");
        arr[1] = new BigDecimal("31.396603");
        trailList.add(arr);

        BigDecimal[] arr2 = new BigDecimal[2];
        arr2[0] = new BigDecimal("120.449794");
        arr2[1] = new BigDecimal("31.396627");
        trailList.add(arr2);

        BigDecimal[] arr3 = new BigDecimal[2];
        arr3[0] = new BigDecimal("120.449980");
        arr3[1] = new BigDecimal("31.396627");
        trailList.add(arr3);

        BigDecimal[] arr4 = new BigDecimal[2];
        arr4[0] = new BigDecimal("120.450149");
        arr4[1] = new BigDecimal("31.396780");
        trailList.add(arr4);

        BigDecimal[] arr5 = new BigDecimal[2];
        arr5[0] = new BigDecimal("120.450357");
        arr5[1] = new BigDecimal("31.396929");
        trailList.add(arr5);

        BigDecimal[] arr6 = new BigDecimal[2];
        arr6[0] = new BigDecimal("120.450497");
        arr6[1] = new BigDecimal("31.396874");
        trailList.add(arr6);

        BigDecimal[] arr7 = new BigDecimal[2];
        arr7[0] = new BigDecimal("120.450568");
        arr7[1] = new BigDecimal("31.396806");
        trailList.add(arr7);

        BigDecimal[] arr8 = new BigDecimal[2];
        arr8[0] = new BigDecimal("120.450676");
        arr8[1] = new BigDecimal("31.396728");
        trailList.add(arr8);

        BigDecimal[] arr9 = new BigDecimal[2];
        arr9[0] = new BigDecimal("120.450759");
        arr9[1] = new BigDecimal("31.396662");
        trailList.add(arr9);

        BigDecimal[] arr10 = new BigDecimal[2];
        arr10[0] = new BigDecimal("120.450875");
        arr10[1] = new BigDecimal("31.396574");
        trailList.add(arr10);

        //实时定位
        BigDecimal[] arr11 = new BigDecimal[2];
        arr11[0] = new BigDecimal("120.451263");
        arr11[1] = new BigDecimal("31.397944");
        positionList.add(arr11);

        BigDecimal[] arr12 = new BigDecimal[2];
        arr12[0] = new BigDecimal("120.451760");
        arr12[1] = new BigDecimal("31.397339");
        positionList.add(arr12);

        BigDecimal[] arr13 = new BigDecimal[2];
        arr13[0] = new BigDecimal("120.452721");
        arr13[1] = new BigDecimal("31.398215");
        positionList.add(arr13);

        BigDecimal[] arr14 = new BigDecimal[2];
        arr14[0] = new BigDecimal("120.453868");
        arr14[1] = new BigDecimal("31.397747");
        positionList.add(arr14);

        BigDecimal[] arr15 = new BigDecimal[2];
        arr15[0] = new BigDecimal("120.450637");
        arr15[1] = new BigDecimal("31.398514");
        positionList.add(arr15);

        BigDecimal[] arr16 = new BigDecimal[2];
        arr16[0] = new BigDecimal("120.450814");
        arr16[1] = new BigDecimal("31.396034");
        positionList.add(arr16);

        BigDecimal[] arr17 = new BigDecimal[2];
        arr17[0] = new BigDecimal("120.451692");
        arr17[1] = new BigDecimal("31.398008");
        positionList.add(arr17);

        BigDecimal[] arr18 = new BigDecimal[2];
        arr18[0] = new BigDecimal("120.453021");
        arr18[1] = new BigDecimal("31.397747");
        positionList.add(arr18);

        BigDecimal[] arr19 = new BigDecimal[2];
        arr19[0] = new BigDecimal("120.454923");
        arr19[1] = new BigDecimal("31.398098");
        positionList.add(arr19);



    }


    @GetMapping("/BaseInfo")
    @Log("基本人员信息")
    @ApiOperation("基本人员信息")
    public ResponseEntity<Object> getBaseInfo() {
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msgInfo", "");
        JSONArray array = new JSONArray();
        for (int i = 0; i < 15; i++) {
            JSONObject object = new JSONObject();
            object.put("cid", String.valueOf(i + 1));
            object.put("type", 2);
            object.put("name", "苏U" + (i < 10 ? "0" : "") + i + "999");
            array.add(object);
        }

        for (int i = 15; i < 30; i++) {
            JSONObject object = new JSONObject();
            object.put("cid", String.valueOf(i + 1));
            object.put("type", 1);
            object.put("name", (i+1) + "号员工");
            array.add(object);
        }
        result.put("BaseInfo", array);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    @GetMapping("/Location")
    @Log("当前人员定位")
    @ApiOperation("当前人员定位")
    public ResponseEntity<Object> getLocation() {
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msgInfo", "");
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("cid", "1");
        object.put("speed", 12);
        Date now = new Date();
        long timestamp = now.getTime() / 1000;
        object.put("coord", trailList.get((int)(timestamp % 20)/2));
        array.add(object);

        for (int i = 0; i < positionList.size(); i++) {
            JSONObject object2 = new JSONObject();
            object2.put("cid", String.valueOf(i*3 + 3));
            object2.put("speed", i + 5);
            object2.put("coord", positionList.get(i));
            array.add(object2);
        }

        result.put("Location", array);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @GetMapping("/HistoryLocation")
    @Log("获取定位轨迹")
    @ApiOperation("获取定位轨迹")
    public ResponseEntity<Object> getHistory(String cid, String beginDate, String endDate) {
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msgInfo", "");
        JSONArray array = new JSONArray();
        if (!StringUtils.equalsAny(cid, "1", "2", "3")) {
            result.put("HistoryLocation", array);
            return new ResponseEntity<>(result, HttpStatus.OK);
        }

        JSONObject object = new JSONObject();
        object.put("cid", cid);
        Date begin = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        JSONArray trajectory = new JSONArray();
        trailList.forEach(position -> {
            JSONObject item = new JSONObject();
            item.put("coord", position);
            item.put("t", DateUtil.format(calendar.getTime(), DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS));
            trajectory.add(item);
            calendar.add(Calendar.SECOND, 2);
        });
        object.put("trajectory", trajectory);

        array.add(object);
        result.put("HistoryLocation", array);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
